// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/expression/string_writer.h>
#include <pollux/type/timestamp.h>

namespace kumo::pollux::exec {
    enum PolicyType { LegacyCastPolicy = 1, PrestoCastPolicy, SparkCastPolicy };

    fmt::underlying_t<PolicyType> format_as(PolicyType f);

    /// This class provides cast hooks to allow different behaviors of CastExpr and
    /// SparkCastExpr. The main purpose is to create customized cast implementation
    /// by taking full usage of existing cast expression.
    class CastHooks {
    public:
        virtual ~CastHooks() = default;

        virtual Expected<Timestamp> castStringToTimestamp(
            const StringView &view) const = 0;

        virtual Expected<Timestamp> castIntToTimestamp(int64_t seconds) const = 0;

        virtual Expected<std::optional<Timestamp> > castDoubleToTimestamp(
            double seconds) const = 0;

        virtual Expected<int32_t> castStringToDate(
            const StringView &dateString) const = 0;

        // 'data' is guaranteed to be non-empty and has been processed by
        // removeWhiteSpaces.
        virtual Expected<float> castStringToReal(const StringView &data) const = 0;

        // 'data' is guaranteed to be non-empty and has been processed by
        // removeWhiteSpaces.
        virtual Expected<double> castStringToDouble(const StringView &data) const = 0;

        // Trims all leading and trailing UTF8 whitespaces.
        virtual StringView removeWhiteSpaces(const StringView &view) const = 0;

        // Returns the options to cast from timestamp to string.
        virtual const TimestampToStringOptions &timestampToStringOptions() const = 0;

        // Returns whether to cast to int by truncate.
        virtual bool truncate() const = 0;

        virtual PolicyType getPolicy() const = 0;
    };
} // namespace kumo::pollux::exec
